package com.sctu.http;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class HttpGetBitmap {
    private String imageURL = "";
    private static HttpGetBitmap httpGetBitmap = null;

    /**
     * 构造函数
     * @param imageURL 图片请求地址
     */
    public HttpGetBitmap(String imageURL){
        this.imageURL = imageURL;
    }

    /**
     * 获取实例 单列模式
     *
     * @return
     */
    public static HttpGetBitmap getInstance(String imageURL) {
        if (httpGetBitmap == null) {
            httpGetBitmap = new HttpGetBitmap(imageURL);
        }
        return httpGetBitmap;
    }

    /**
     * 获取Bitmap
     * @return
     */
    public Bitmap getBitmap(){
        Bitmap bitmap = null;
        try {
            URL URLimage = new URL(imageURL);
            URLConnection urlConnection = URLimage.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.connect();
            InputStream in = urlConnection.getInputStream();
            bitmap = BitmapFactory.decodeStream(in);
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

}
